package tu.java.projection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Vector;

import tu.java.db.DBConnManager;
import tu.java.hall.Hall;
import tu.java.movie.Movie;
import tu.java.utils.DateUtils;

public class Projection {
	
	
	
	private int		projectionId;
	private Hall	hall;
	private Movie	movie;
	private String	projectionBeginDateTime;
	private String	projectionEndDateTime;
	
	
	
	public Projection() {
		
	}
	
	
	
	public Projection(Movie movie, Hall hall, String projectionBeginDateTime) throws ParseException {
		this.movie = movie;
		this.hall = hall;
		this.projectionBeginDateTime = projectionBeginDateTime;
		
		Calendar begin = Calendar.getInstance();
		begin.setTime(DateUtils.sdfYMD_HM().parse(projectionBeginDateTime));
		
		Calendar end = Calendar.getInstance();
		end.setTimeInMillis(begin.getTimeInMillis() + (movie.getMovieLenght() * 60 * 1000));
		
		this.projectionEndDateTime = DateUtils.sdfYMD_HM().format(end.getTime());
	}
	
	
	
	public boolean addProjection(int userId) {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" INSERT INTO TP01_PROJECTIONS (CM01_MOVIE_ID,CH01_HALL_ID,CP01_BEGIN_TIME_DATE,CP01_END_TIME_DATE,CS02_USER_ID)");
			sql.append(" VALUES(?,?,?,?,?)");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, movie.getMovieId());
			pstmt.setInt(2, hall.getHallId());
			pstmt.setObject(3, projectionBeginDateTime);
			pstmt.setObject(4, projectionEndDateTime);
			pstmt.setInt(5, userId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public static Vector<Projection> searchProjections(int movieId, int hallId, String beginTime, String endTime, int projectionId) {
		Vector<Projection> projections = new Vector<Projection>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT CP01_PROJECTION_ID,CM01_MOVIE_ID,CH01_HALL_ID,CP01_BEGIN_TIME_DATE,CP01_END_TIME_DATE FROM TP01_PROJECTIONS");
			
			if (projectionId == 0) {
				sql.append(" where CP01_BEGIN_TIME_DATE >= ?");
				sql.append(" and CP01_END_TIME_DATE <= ?");
				
				if (movieId != 0) {
					sql.append(" AND CM01_MOVIE_ID=?");
				}
				
				if (hallId != 0) {
					sql.append(" AND CH01_HALL_ID=?");
				}
			} else {
				sql.append(" where CP01_PROJECTION_ID = ?");
			}
				sql.append(" ORDER BY CM01_MOVIE_ID");
			pstmt = conn.prepareStatement(sql.toString());
			
			if (projectionId == 0) {
				if (beginTime == null || (beginTime != null && beginTime.equals(""))) {
					beginTime = "1900-01-01 00:00:00";
				}
				if (endTime == null || (endTime != null && endTime.equals(""))) {
					endTime = "2099-01-01 00:00:00";
				}
				pstmt.setObject(1, beginTime);
				pstmt.setObject(2, endTime);
				
				if (movieId != 0) {
					pstmt.setInt(3, movieId);
				}
				if (hallId != 0) {
					if (movieId != 0) {
						pstmt.setInt(4, hallId);
					}
					else {
						pstmt.setInt(3, hallId);
					}
				}
			} else {
				pstmt.setInt(1, projectionId);
			}
			
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Projection p = new Projection();
				p.projectionId = rs.getInt(1);
				p.movie = Movie.searchMovies("", rs.getInt(2)).elementAt(0);
				p.hall = Hall.searchHalls("", rs.getInt(3)).elementAt(0);
				p.projectionBeginDateTime = DateUtils.sdfYMD_HMS().format(rs.getTimestamp(4));
				p.projectionEndDateTime = DateUtils.sdfYMD_HMS().format(rs.getTimestamp(5));
				projections.add(p);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return projections;
	}
	
	
	
	public boolean editProjection(int userId) {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" UPDATE TP01_PROJECTIONS SET CM01_MOVIE_ID=?,CH01_HALL_ID=?,CP01_BEGIN_TIME_DATE=?,CP01_END_TIME_DATE=?,CS02_USER_ID=? WHERE CP01_PROJECTION_ID=?");
			pstmt.setInt(1, movie.getMovieId());
			pstmt.setInt(2, hall.getHallId());
			pstmt.setObject(3, projectionBeginDateTime);
			pstmt.setObject(4, projectionEndDateTime);
			pstmt.setInt(5, userId);
			pstmt.setInt(6, projectionId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public boolean deleteProjection() {
		boolean status = true;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			pstmt = conn.prepareStatement(" DELETE FROM TP01_PROJECTIONS WHERE CP01_PROJECTION_ID=?");
			pstmt.setInt(1, projectionId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public int getProjectionId() {
		return projectionId;
	}
	
	
	
	public void setProjectionId(int projectionId) {
		this.projectionId = projectionId;
	}
	
	
	
	public Hall getHall() {
		return hall;
	}
	
	
	
	public void setHall(Hall hall) {
		this.hall = hall;
	}
	
	
	
	public Movie getMovie() {
		return movie;
	}
	
	
	
	public void setMovie(Movie movie) {
		this.movie = movie;
	}
	
	
	public String getProjectionBeginDateTime() {
		return projectionBeginDateTime;
	}
	
	
	
	public void setProjectionBeginDateTime(String projectionBeginDateTime) {
		this.projectionBeginDateTime = projectionBeginDateTime;
	}
	
	
	public String getProjectionEndDateTime() {
		return projectionEndDateTime;
	}
	
	
	
	public void setProjectionEndDateTime(String projectionEndDateTime) {
		this.projectionEndDateTime = projectionEndDateTime;
	}
	
}
